package com.coamc.xlsunit;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.NameConversion;
import org.beetl.sql.core.SQLManager;

public class RowOrmOutputHolder extends  RowOutputHolder {
	static Log log = LogFactory.getLog(RowOrmOutputHolder.class);

	public RowOrmOutputHolder(XLSParser parser, VariableTable vars, String sheetName) {
		super(parser,vars,sheetName);
	}

	
	@Override
	protected void fetchAndCompareRecords(List<Map<String, Object>> allRecords,
			List<String> compare, String sql, List<DBCallBack> cbs,XLSPoistion p) {
		try{
			DBAccess access = parser.db;

			List<Map<String,Object>> dbList = null;
			try {

				dbList = access.query(tableName,sql,vars);
			} catch (RuntimeException ex) {
				throw new RuntimeException(" 查询数据" + this.tableName + " 出错，在" + p, ex);
			}

			org.junit.Assert.assertEquals("查询结果总数不匹配 "+p.toString(), allRecords.size(), dbList.size());
			if (allRecords.size() == dbList.size()) {
				for (int i = 0; i < allRecords.size(); i++) {
					Map<String,Object> excelData = allRecords.get(i);
					Map<String,Object>  dbData = dbList.get(i);
					CompareInfo info = new CompareInfo();
					boolean isSame = compare(excelData, dbData,  compare,info);
					if (!isSame) {
						org.junit.Assert.fail("比较数据 出错，在第"+(i+1)+"行，位于"+ p+",错误信息："+info.toString());
					}
					DBCallBack cb = cbs.get(i);
					if(cb.hasData()){
						for(DBCallBackItem item:cb.varRef){
							String colName = item.getColName();
							vars.add(item.getVarRef(), dbData.get(colName));
						}
					}
				}
			}

		}catch(RuntimeException ex){
			log.fatal("保存数据出错,位置是"+p,ex);
			throw ex;
		}
		
	}

	/**
	 * 按照主键
	 * 
	 * @param record
	 * @param queryKeys
	 * @param line
	 * @param file
	 */
	@Override
	protected void fetchAndCompareOne(Map<String, Object> record, Map<String, Object> queryKeys, List<String> compare,
			DBCallBack cb,XLSPoistion p) {
		// 开始存入数据库
		try{
			
			DBAccess access = parser.db;

			Map<String,Object>  dbData = null;
			try {
				dbData = access.findById(tableName, queryKeys);
				if (dbData == null) {
					org.junit.Assert.fail("比较数据 出错，在" + p+" 根据 "+queryKeys+" 找不到数据");
					return;
				}

			} catch (RuntimeException ex) {
				throw new RuntimeException(" 查询数据" + this.tableName + " 出错，在" + p, ex);
			}
			CompareInfo info = new CompareInfo();
			boolean isSame = compare(record,dbData, compare,info);
			if (!isSame) {
				org.junit.Assert.fail("比较数据 出错，在" + p+" 错误信息："+info.toString());
			}
			
			if(cb.hasData()){
				for(DBCallBackItem item:cb.varRef){
					String colName = item.getColName();
					Object o = dbData.get(colName);
					vars.add(item.getVarRef(), o);
				}
			}
			
		}catch(RuntimeException ex){
			log.fatal("保存数据出错,位置是"+p,ex);
			throw ex;
		}
		

	}
	
	public  boolean compare(Map<String,Object> a,Map<String,Object> b,List<String> cols,CompareInfo info){
		for(String col:cols){
			
			try {
				
				Object o1 = a.get(col);
				Object o2 =b.get(col);
				if(!ClassUtil.isSame(o1,o2)){
					info.setColName(col);
					info.setExpected(o1);
					info.setReal(o2);
					return false;
				}
		
			} catch (Exception e) {
				throw new RuntimeException("比较数据 出错,未找到方法"+col,e);
			}
							
		}
		return true;
		
	}

}
